package com.work.game.ad.kaku.banner;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.anythink.banner.api.ATBannerExListener;
import com.anythink.banner.api.ATBannerView;
import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.ATAdStatusInfo;
import com.anythink.core.api.ATNetworkConfirmInfo;
import com.anythink.core.api.AdError;
import com.work.game.ad.impl.AdRevenueListenerImpl;
import com.work.game.home.ui.enums.TaKuAdPositionEnum;

import cn.hutool.core.util.ObjectUtil;

/**
 * 横幅广告
 */
public class TaKuBannerAdOBJ implements ATBannerExListener {

    private static  String TAG ="TaKuBannerAd";
    private ATBannerView mBannerView;
    private String token;
    private ViewGroup viewGroup;

    public TaKuBannerAdOBJ() {
    }



    public void load(Context context, String placementID, ViewGroup frameLayout) {
        this.viewGroup = frameLayout;
        viewGroup.setVisibility(View.VISIBLE);
        mBannerView = new ATBannerView(context);
        mBannerView.setPlacementId(placementID);
        int width = context.getResources().getDisplayMetrics().widthPixels;//定一个宽度值，比如屏幕宽度
        int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        //如果出现Banner有时高、有时低的情况，请使用此代码
        //float ratio = 320/50f;//必须跟Taku后台配置的Banner广告源宽高比例一致，假设尺寸为320x50
        //int width = getResources().getDisplayMetrics().widthPixels;//定一个宽度值，比如屏幕宽度
        //int height = (int) (width / ratio);
        mBannerView.setLayoutParams(new FrameLayout.LayoutParams(width, height));
        mBannerView.setBannerAdListener(this);
        mBannerView.setAdRevenueListener( new AdRevenueListenerImpl(context,token, TaKuAdPositionEnum.AD_BANNER_POSITION.getKey(), "6"));
        mBannerView.loadAd();
    }


    @Override
    public void onDeeplinkCallback(boolean b, ATAdInfo atAdInfo, boolean b1) {

    }

    @Override
    public void onDownloadConfirm(Context context, ATAdInfo atAdInfo, ATNetworkConfirmInfo atNetworkConfirmInfo) {

    }

    @Override
    public void onBannerLoaded() {
        viewGroup.setVisibility(View.VISIBLE);
        ATAdStatusInfo atAdStatusInfo =  mBannerView.checkAdStatus();
        if(!atAdStatusInfo.isReady()){
           return;
        }

        if(ObjectUtil.isNotNull(viewGroup)){
            viewGroup.removeAllViews();
        }
        viewGroup.addView(mBannerView);

    }

    @Override
    public void onBannerFailed(AdError adError) {
        Log.i(TAG, "onBannerFailed: "+adError.getFullErrorInfo());
    }

    @Override
    public void onBannerClicked(ATAdInfo atAdInfo) {
        Log.i(TAG, "onBannerClicked: ");
    }

    @Override
    public void onBannerShow(ATAdInfo atAdInfo) {
        Log.i(TAG, "onBannerShow: ");
    }

    @Override
    public void onBannerClose(ATAdInfo atAdInfo) {
        if (mBannerView != null && mBannerView.getParent() != null) {
            ((ViewGroup) mBannerView.getParent()).removeView(mBannerView);
        }
    }

    @Override
    public void onBannerAutoRefreshed(ATAdInfo atAdInfo) {
        Log.i(TAG, "onBannerAutoRefreshed");
    }

    @Override
    public void onBannerAutoRefreshFail(AdError adError) {
        Log.i(TAG, "onBannerAutoRefreshFail: "+adError.getFullErrorInfo());
    }

    public void setToken(String token) {
        this.token = token;
    }
}
